<?php

namespace Agent\Controller;
use Common\Controller\AgentbaseController;

class DataController extends AgentbaseController {
    
    
    function _initialize() {
        parent::_initialize();      
    }
    
    public function mem_game_filter(&$where){
        if(isset($_GET['mem_name'])&&$_GET['mem_name']!=''){
            $v=  $_GET['mem_name'];
            $where["m.username"]=array("like","%$v%");  
        }
        
        if(isset($_GET['game_name'])&&$_GET['game_name']!=''){            
            $gamename=$_GET['game_name'];
            $where["g.name"]=array("like","%$gamename%"); 
        }
    }
    
     public function getgameptbcheck(){
        $agent_id=$_SESSION['agent_id'];

        $model=M("gm_pay");
        
        $where=array();
        $where['gp.agent_id']=$agent_id;


        $this->mem_game_filter($where);
        

        $count=$model
                ->alias('gp')
                ->join("LEFT JOIN ".C('DB_PREFIX')."game g ON g.id=gp.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gp.mem_id")
                ->where($where)->count();
        $page=new \Think\Page($count,5);
                
        $records=$model
                ->field("gp.*,g.name as gamename,m.username as memname")
                ->alias('gp')
                ->join("LEFT JOIN ".C('DB_PREFIX')."game g ON g.id=gp.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gp.mem_id")
                ->where($where)
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $sumitems=$model
                ->field("sum(gm_cnt) as sum_cnt")
                ->alias('gp')
                ->join("LEFT JOIN ".C('DB_PREFIX')."game g ON g.id=gp.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gp.mem_id")
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        $this->assign("formget",$_GET);
        
        $this->assign("num_of_records",$count);
        $this->assign("Page", $page->show());
        $this->assign("records",$records);
        $this->display();
    }

    public function getuserptbvalidityduration(){       
        
        $agent_id=$_SESSION['agent_id'];
        $model=M('gm_mem');
        
        $where=array();
        $where['m.agent_id']=$agent_id;


        $this->mem_game_filter($where);
        
        $count=$model->alias('gc')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->join("INNER JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")->where($where)->count();
        $page=new \Think\Page($count,5);

        $items=$model
                ->field("gc.*,m.username as memname,g.name as gamename")
                ->alias('gc')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->join("INNER JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                ->where($where)
                ->order("gc.update_time desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $sumitems=$model
                ->field("sum(remain) as sum_remain")
                ->alias('gc')
                ->join("INNER JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        $this->assign("formget",$_GET);
        
        $this->assign("num_of_records",$count);
        $this->assign("Page", $page->show());
        $this->assign("records",$items);
        $this->display();
        
    }
    
     public function getBalanceChargeRecord(){
        
        $agent_id=$_SESSION['agent_id'];
        $where=array();
        $where['gac.agent_id']=$agent_id;        
        
        if(isset($_GET['s_time'])&&$_GET['s_time']!=''){
            $where["gac.create_time"]=array('gt',strtotime($_GET['s_time']));
        }
        
        if(isset($_GET['e_time'])&&$_GET['e_time']!=''){
            $where["gac.create_time"]=array('lt',strtotime($_GET['e_time']));
        }
        
        $model=M("gm_agentcharge");
        $count=$model->alias('gac')->where($where)->count();
        $page=new \Think\Page($count,10);
        
//        $items=M('gm_agentcharge')
//                ->field("ac.*,u.user_nicename,u.mobile,u.user_login,g.name as gamename")
//                ->alias('ac')
//                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.agent_id")
//                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
//                ->order("ac.id desc")
//                ->limit($page->firstRow . ',' . $page->listRows)
//                ->select();               
        
        $records=$model
                 ->field("gac.*,u.user_login")
                ->alias('gac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=gac.admin_id")
                ->where($where)
                ->limit($page->firstRow . ',' . $page->listRows)
                ->order("gac.id desc")
                ->select();
        
        foreach($records as $k => $record){
            if($record['admin_id']==0){
                $records[$k]['money']="<span style='color:red;'>-".$records[$k]['money']."</span>";
            }else{
                $records[$k]['money']="<span style='color:green;'>+".$records[$k]['money']."</span>";
            }
        }
        
        $sumitems=$model
                ->field("sum(money) as sum_money")
                ->alias('gac')
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        $this->assign("formget",$_GET);
        
        $this->assign("Page", $page->show());
        $this->assign("num_of_records",$count);
        $this->assign("records",$records);
        $this->display();
    }
    
    public function set_user_type(&$items){
        foreach($items as $key=>$member){
            $type=$items[$key]['user_type'];
            if($type==$this->agent_roleid){
                $items[$key]['user_type']='代理';
            }else if($type==$this->subagent_roleid){
                $items[$key]['user_type']='下级代理';
            }
        }
    }
    
     public function getUserPayment(){
        
        $agent_id=$_SESSION['agent_id'];
        $where=array();
        
        $subids_txt=$this->get_subids_txt();
        if(!$subids_txt){
            $where['_string']="gc.admin_id=$agent_id";
        }else{
            $where['_string']="gc.admin_id=$agent_id OR gc.admin_id IN ($subids_txt)";
        }
        

        $this->mem_game_filter($where);
        
        
        $count=M('gm_charge')
                ->alias('gc')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=gc.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                ->where($where)
                ->count();
        $page=new \Think\Page($count,10);

        $items=M('gm_charge')
                ->field("gc.*,u.user_nicename,u.mobile,u.user_login,m.username as memname,g.name as gamename,u.user_type,gc.admin_id")
                ->alias('gc')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=gc.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                ->where($where)
                ->order("gc.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        $this->set_user_type($items);
        
        $sumitems=M('gm_charge')
                ->field("sum(gm_cnt) as sum_cnt")
                ->alias('gc')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        
        $this->assign("formget",$_GET);
        
        $this->assign("num_of_records",$count);
        $this->assign("Page", $page->show());
        $this->assign("records",$items);
        $this->display();
    }
    
    public function get_subids_txt(){
        $subids=$this->huoshu_agent->getMySubAgentsIds();
        if(!$subids){
            return false;
        }
        $subids_txt=join(",",$subids);
        
        return $subids_txt;
    }
    
    public function member(){
        $model=M("members");
//        $where=array("m.agent_id"=>$_SESSION['agent_id']);
        
        $subids=$this->huoshu_agent->getMySubAgentsIds();
        array_push($subids,$this->agid);
        $subids_txt=join(",",$subids);
        $where=array();
         $where['_string']="m.agent_id IN ($subids_txt)";
         
         if(isset($_GET['mem_name'])&&$_GET['mem_name']){
             $v=$_GET['mem_name'];
             $where['m.username']=array("like","%$v%");
         }
         
         $hs_where_obj=new \HuoShu\Where();
         $hs_where_obj->time($where,"m.reg_time");
        
        
        $count=$model->alias('m')
                ->join("LEFT JOIN ".C('DB_PREFIX')."users u ON u.id=m.agent_id" )
                ->where($where)
                ->count();
        
        $Page= new \Think\Page($count,10);                            
        $show = $Page->show();// 分页显示输出   
               
        $members=$model
                ->field("m.*,g.name as gamename,u.user_type,u.user_nicename")
                ->alias('m')
                ->join("LEFT JOIN ".C('DB_PREFIX')."game g ON g.id=m.app_id" )
                ->join("LEFT JOIN ".C('DB_PREFIX')."users u ON u.id=m.agent_id" )
                ->where($where)                
                ->order("m.reg_time desc")
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        
        foreach($members as $key=>$member){
            $type=$members[$key]['user_type'];
            if($type==$this->agent_roleid){
                $members[$key]['user_type']='代理';
            }else if($type==$this->subagent_roleid){
                $members[$key]['user_type']='下级代理';
            }
        }
        
        $n=count($members);
        $this->assign("n",$n);
        $this->assign("page", $show);
        $this->assign("members",$members);
        
        $this->assign("formget",$_GET);
        
        $this->assign("page_title","玩家注册明细");
        $this->display();
    }
    
    public function normalChargeRecord(){
        
        $ids=$this->huoshu_agent->getMySubAgentsIds();
        $ids[]=$this->agid;
        $ids_txt=join(",",$ids);
        
        $where=array();
        $where['_string']="p.agent_id IN ($ids_txt) AND (p.status=2) AND (p.payway!='gamepay') AND (p.payway!='ptbpay')";
        
        $this->mem_game_filter($where);
        
        
        
        $field = "p.order_id, p.amount, m.username,p.agent_id, p.payway, u.user_login agentname, "
                . "u.user_nicename agentnickname, g.name gamename,p.status,p.cpstatus, p.create_time, p.app_id";
        $count=M('pay')
                ->alias("p")
                ->where($where)
                ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
                ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
                ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
                ->count();
        $page=new \Think\Page($count,10);
        
        $items = M('pay')
        ->alias("p")
        ->field($field)
        ->where($where)
        ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->order("p.id DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
        
        
        $sum_field="sum( p.amount) as sum_amount";
        $sumitems = M('pay')
        ->alias("p")
        ->field($sum_field)
        ->where($where)
        ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->order("p.id DESC")
        ->select();
        
        
        $this->assign("formget",$_GET);
        $this->assign("sum_value",$sumitems[0]);
        
        $this->_payway();
        $this->_cpstatus();
        $this->_paystatus();
        
        $this->assign("num_of_records",$count);
        $this->assign("Page", $page->show());
        $this->assign("orders",$items);
        
        
        $this->display();
    }
    
    function _payway($type=NULL, $option=true){
        $cates=array(
                ""=>"全部"
        );
        $payways = M('payway')->getField("payname,realname", true);
        if($option){
            $payways=$cates + $payways;
        }
        
        $this->assign("payways",$payways);
    }
    
    function _paystatus(){
        $cates=array(
                "0"=>"全部",
                "1"=>"待支付",
                "2"=>"支付成功",                
                "3"=>"支付失败",
        );
        $this->assign("paystatuss",$cates);
    }
    
    function _cpstatus(){
        $cates=array(
                "0"=>"全部",
                "1"=>"待支付",
                "2"=>"回调成功",
                "3"=>"回调失败",
        );
        $this->assign("cpstatuss",$cates);
    }
    
    public function memGmRechargeRecord(){
               

//        $hs_data_ptbmem_obj=new \HuoShu\Data\PtbMem();
//       
//        $table_header=$hs_data_ptbmem_obj->get_table_header_agent();
//        $this->assign("table_header",$table_header);       
//        
//        $count=$hs_data_ptbmem_obj->get_agent_count($this->agid);
//        $page=new \Think\Page($count,10);                
//        $table_content=$hs_data_ptbmem_obj->get_agent_table($this->agid,$page->firstRow,$page->listRows);
//        $this->assign("table_content",$table_content);
//        
//        $this->assign("Page", $page->show());
//        
//        $sum=$hs_data_ptbmem_obj->get_sum_agent_txt($this->agid);
//        $this->assign("table_sum",$sum);
        $where=array();
        $hs_agent_obj=new \HuoShu\Agent($this->agid);
        $ids=$hs_agent_obj->GetMeAndMySubAgentIDs();
        $ids_txt=join(",",$ids);       
        $where['_string']="gc.admin_id IN ($ids_txt)";
        
        $count=M('gm_charge')
                    ->alias('gc')
                    ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=gc.admin_id")
                    ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                    ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                    ->where($where)
                    ->count();  
        $page=new \Think\Page($count,20);
        $items=M('gm_charge')
                    ->field("gc.order_id,gc.create_time,gc.admin_id,gc.gm_cnt as coin_cnt,gc.payway,gc.real_amount as real_pay,"
                            . "u.mobile,m.username as mem_name,g.name as game_name,u.user_login as agent_name")
                    ->alias('gc')
                    ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=gc.admin_id")
                    ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=gc.app_id")
                    ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=gc.mem_id")
                    ->where($where)
                    ->order("gc.id desc")
                    ->limit($page->firstRow,$page->listRows)
                    ->select();      
        
        $this->assign("items",$items);
        $this->assign("page",$page->show());
        $this->display();
        
    }
    
    public function memChargeRecord(){
        $where=array();
        $this->mem_game_filter($where);
        
        $all_items=$this->getMemChargeItems($where);
        $count=count($all_items);
        $page=new \Think\Page($count,20);
        $items=$this->getMemChargeItems($where,$page->firstRow,$page->listRows);
        

        $this->assign("items",$items);
        $this->assign("formget",$_GET);
        $this->assign("page", $page->show());
        $this->display();
    }
    
    public function getMemChargeItems($where_extra=array(),$start=0,$limit=0){
        $hs_agent_obj=new \HuoShu\Agent($this->agid);
        $ids_txt=$hs_agent_obj->getMeAndMySubIdsTxt();
        $where=array();
        $where['_string']="p.agent_id IN ($ids_txt) ";
        $where['p.status']=2;
        $items=M('pay')
                ->field("p.create_time,p.amount,p.status,p.order_id,p.payway,p.agent_id,"
                        . "u.user_login as agent_name,m.username as mem_name,g.name as game_name,u.user_type")
                ->alias('p')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=p.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=p.mem_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=p.app_id")
                ->order("p.id desc")
                ->where($where)
                ->where($where_extra)
                ->limit($start,$limit)
                ->select();
        
        $payway_data=M('payway')->getField("payname,realname",true);
        foreach($items as $k=>$v){
            $items[$k]['payway_txt']=$payway_data[$v['payway']];
            $profit_data=$this->get_agent_gain($v['agent_id'], $v['order_id']);
//            $items[$k]=array_merge($items[$k],$profit_data);     
            $items[$k]['profits']=$profit_data;
        }
        
        $hs_itemsfield_obj=new \HuoShu\Data\ItemsFields();
        $hs_itemsfield_obj->agent_level($items);
        
        return $items;
    }
    
    public function get_agent_gain($agent_id,$order_id){
        $hs_account_obj=new \HuoShu\Account();
        $result=array();
        if($hs_account_obj->is_agent_prev($agent_id)){
            
            $agent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_rate");
            $agent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_gain");
            
            $result['subagent_name']='';
            $result['subagent_profit']='';
            
            $result['agent_name']='('.round($agent_rate,2).')'.$hs_account_obj->getUserName($agent_id);            
            $result['agent_profit']=$agent_gain;
            
            $result['real_amount']=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("real_amount");
            $result['rebate_cnt']=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("rebate_cnt");
            
            if($result['real_amount']){
            if($result['rebate_cnt']!=0){
                $result['benefit_type']="返利";
            }else{
                $result['benefit_type']="折扣";
            }
            }
        }
        
        if($hs_account_obj->is_subagent_prev($agent_id)){
            $subagent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_gain");
            $subagent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_rate");
            $result['subagent_name']='('.round($subagent_rate,2).')'.$hs_account_obj->getUserName($agent_id);
            $result['subagent_profit']=$subagent_gain;
            
            $ownerid=$hs_account_obj->get_parent_agent_id($agent_id);
            
            $agent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$ownerid))->getField("agent_gain");
            $agent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$ownerid))->getField("agent_rate");
            $result['agent_name']='('.round($agent_rate,2).')'.$hs_account_obj->getUserName($ownerid);
            $result['agent_profit']=$agent_gain;
            
            
            $result['real_amount']=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("real_amount");
            $result['rebate_cnt']=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("rebate_cnt");
            
            if($result['real_amount']){
            if($result['rebate_cnt']!=0){
                $result['benefit_type']="返利";
            }else{
                $result['benefit_type']="折扣";
            }
            }
        }
   
        return $result; 
    }
    
    public function get_agent_gain_old($agent_id,$order_id){
        $hs_account_obj=new \HuoShu\Account();
        $result=array();
        if($hs_account_obj->is_agent($agent_id)){
            
            $agent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_rate");
            $agent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_gain");
            
            $result['subagent_name']='';
            $result['subagent_profit']='';
            
            $result['agent_name']='('.round($agent_rate,2).')'.$hs_account_obj->getUserName($agent_id);            
            $result['agent_profit']=$agent_gain;
        }
        
        if($hs_account_obj->is_sub_agent($agent_id)){
            $subagent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_gain");
            $subagent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$agent_id))->getField("agent_rate");
            $result['subagent_name']='('.round($subagent_rate,2).')'.$hs_account_obj->getUserName($agent_id);
            $result['subagent_profit']=$subagent_gain;
            
            $ownerid=$hs_account_obj->get_parent_agent_id($agent_id);
            
            $agent_gain=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$ownerid))->getField("agent_gain");
            $agent_rate=M('agent_order')->where(array("order_id"=>$order_id,"agent_id"=>$ownerid))->getField("agent_rate");
            $result['agent_name']='('.round($agent_rate,2).')'.$hs_account_obj->getUserName($ownerid);
            $result['agent_profit']=$agent_gain;
        }
   
        return $result; 
    }
    
    public function PtbAgentChargeRecord(){
        $hs_data_pac_obj=new \HuoShu\Data\PtbAgentCharge($this->agid);
       
        $table_header=$hs_data_pac_obj->get_table_header();
        $this->assign("table_header",$table_header);   
        
        $where=array();
        $hs_where_obj=new \HuoShu\Where();
        $hs_where_obj->time($where,"pac.create_time");
        
        $count=$hs_data_pac_obj->get_count($where,$this->agid);
        $page=new \Think\Page($count,20);     

        $table_content=$hs_data_pac_obj->get_table($where,$page->firstRow,$page->listRows);
        $this->assign("table_content",$table_content);
        
        $this->assign("Page", $page->show());
        
        $sum=$hs_data_pac_obj->get_sum_txt($where);
        $this->assign("table_sum",$sum);
        
        $this->assign("formget",$_GET);
        $this->display();
    }
            
}

